<template>
  <div v-show="isShow">
    <el-form
    ref="form"
    class="user-form-width"
    :model="form"
    :rules="rules"
    label-width="120px">
      <el-form-item
      prop="oldPassword"
      :label="$t('settings.me.oldPassword')"
    >
      <el-input
        show-password
        :placeholder="$t('settings.me.oldPassword')"
        v-model="form.oldPassword"
      ></el-input>
    </el-form-item>
      <el-form-item
      prop="newPassword"
      :label="$t('settings.me.newPassword')"
    >
      <el-input
        show-password
        :placeholder="$t('settings.me.newPassword')"
        v-model="form.newPassword"
      ></el-input>
    </el-form-item>
      <el-form-item
      prop="reenterPassword"
      :label="$t('settings.me.reenterPassword')"
    >
      <el-input
        show-password
        :placeholder="$t('settings.me.reenterPassword')"
        v-model="form.reenterPassword"
      ></el-input>
    </el-form-item>
      <el-form-item>
        <button type="button" role="primary" @click="submit">确认修改</button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { queryLoginUser, updateUserPassword } from '@/assets/api/index'
const BLUR = 'blur'
export default {
  data () {
    return {
      isShow: false,
      form: {},
      rules: {
        oldPassword: [
          { required: true, message: this.$t('validationMessage.inputRequired'), trigger: BLUR },
          { min: 6, max: 12, trigger: BLUR, message: this.$t('validationMessage.lengthRange', { min: 6, max: 12 }) },
          { validator: this.validateOldPassword, trigger: BLUR }
        ],
        newPassword: [
          { required: true, message: this.$t('validationMessage.inputRequired'), trigger: BLUR },
          { min: 6, max: 12, trigger: BLUR, message: this.$t('validationMessage.lengthRange', { min: 6, max: 12 }) }
        ],
        reenterPassword: [
          { required: true, message: this.$t('validationMessage.inputRequired'), trigger: BLUR },
          { min: 6, max: 12, trigger: BLUR, message: this.$t('validationMessage.lengthRange', { min: 6, max: 12 }) },
          { validator: this.validateReenterPassword, trigger: BLUR }
        ]
      }
    }
  },
  props: { account: { type: String } },
  methods: {
    toggle () {
      this.isShow ? this.hidden() : this.show()
    },
    show () { this.isShow = true },
    hidden () {
      this.$refs.form.clearValidate()
      this.form = {}
      this.isShow = false
    },
    validateReenterPassword (rules, value, callback) {
      if (value === this.form.newPassword) {
        callback()
      } else {
        callback(new Error(this.$t('settings.me.reenterPasswordValidationMessage')))
      }
    },
    validateOldPassword (rules, value, callback) {
      queryLoginUser({ password: value }).then(res => {
        if (res.data !== null && typeof res.data === 'object' && res.data.account === this.account) {
          callback()
        } else {
          callback(new Error(this.$t('settings.me.wrongPassword')))
        }
      })
    },
    submit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          updateUserPassword(this.form).then(res => {
            if (res.data === 'failed') {
              this.$message({ type: 'warning', message: this.$t('common.operationFailed') })
            } else {
              this.$message({ type: 'success', message: this.$t('common.saved') })
            }
          })
        }
      })
    }
  }
}
</script>
